package cn.m3a1.task;

import cn.m3a1.dao.UserDao;
import cn.m3a1.model.Course;
import cn.m3a1.model.Studentofclass;
import cn.m3a1.model.Tclass;
import cn.m3a1.model.User;
import com.iceking.dynamicweb.*;
import net.sf.json.JSONObject;

import java.util.List;

/**
 * Created by wyj on 2019-06-03.
 */
public class CourseTask extends TaskProcessor {
    private User loginUser;
    public Boolean panduan(){
        loginUser = (User) getSession().getAttribute("loginUser");
        if (loginUser==null){
            return false;
        }

        int type=loginUser.getType();

        if (type!=99){
            return false;
        }
        return  true;
    }
    /**
     * 课程管理页
     * @throws Exception
     */
    public void home() throws  Exception{
        if (!panduan()){
            return;
        }
        String name = getParam("name");
        int classid = getParamInt("type");
        FrmkView frmkView;
        UserDao userDao=new UserDao();
        User loginUser = (User)getSession().getAttribute("loginUser");


            frmkView=new FrmkView("course.html");
            //查询所有班级信息
            getDBConn().startTransaction();
            Pagination<Course> pg = new Pagination<>();
            pg.appendWhere("where 1=1 ");
            //判断是否有查询条件
            if (!GlobalUtils.isEmpty(name)){
                pg.appendWhere(" and name=?",name);
            }
            if (classid!=0){
                pg.appendWhere("and classid=?",classid);
            }

//            if (!GlobalUtils.isEmpty(name)&&!name.equals("0")){
//                pg.appendWhere("where name=?",name);
//            }
            getDBConn().select(Course.class, pg);
            List list = pg.getRows();


            //查询教师信息
            Pagination<User> pgu = new Pagination<>();
            pgu.appendWhere("where type=?",50);

            getDBConn().select(User.class, pgu);
            List lius = pgu.getRows();

            //查询班级信息
            Pagination<Tclass> pgt = new Pagination<>();
            //pgu.appendWhere("where type=?",50);

            getDBConn().select(Tclass.class, pgt);
            List tclasses = pgt.getRows();
            getDBConn().stopTransaction();

            frmkView.addData("members",list);
            frmkView.addData("lius",lius);
            frmkView.addData("tclasses",tclasses);



        frmkView.addData("loginUser",loginUser);
        toFreemarker(frmkView);
    }
    public void findcourse() throws Exception {
        JSONObject json = new JSONObject();

        int id = getParamInt("id");
        System.out.println("id=" + id);
        //根据用户名和密码去进行数据库访问
        Pagination<Course> pg = new Pagination<>();
        pg.appendWhere("where id=?", id);
        getDBConn().select(Course.class, pg);
        Course course = pg.getFirstResult();
        if (course != null) {

            json.put("succ", true);
            json.put("course", course);
            toJson(json.toString());
        }
    }
    /**
     * 课程表修改
     *
     * @throws Exception
     */
    public void update() throws Exception {
        JSONObject json = new JSONObject();

        int id=getParamInt("id");
        int classid=getParamInt("classid");
        int teacherid=getParamInt("teacherid");
        String room=getParam("room");
        String time = getParam("time");
        Pagination<Course> pg = new Pagination<>();
        pg.appendWhere("where id=?", id);
        getDBConn().select(Course.class, pg);
        Course course = pg.getFirstResult();
        course.setRoom(room);
        course.setTime(time);
        course.setClassid(classid);
        course.setTeacherid(teacherid);
        getDBConn().save(course);
        json.put("succ", true);
        json.put("stmt", "更改成功");
        toJson(json.toString());
    }
    /**
     * 根据id删除数据
     * @throws Exception
     */
    public void del() throws Exception {
        int id = getParamInt("id");
        System.out.println("del"+id);
            Pagination<Course> pg = new Pagination<>();
            pg.appendWhere("where id=?", id);
            getDBConn().select(Course.class, pg);
            Course course = pg.getFirstResult();
            getDBConn().delete(course);

    }
    /**
     * 保存用户
     * @throws Exception
     */
    public void save() throws Exception {
        JSONObject json = new JSONObject();
         Course course=new Course();
        course.setSemyear1(getParamInt("semyears1"));
        course.setSemyear2(getParamInt("semyears2"));
        course.setRoom(getParam("room"));
        course.setSemno(getParamInt("semno"));
        course.setClassid(getParamInt("classid"));
        course.setTeacherid(getParamInt("teacherid"));
        course.setTime(getParam("time"));
        course.setName(getParam("name"));

            //保存课程信息
            DBConnection connection = DWPConsole.getInstance().getOneConnection();
            connection.save(course);
            connection.close();

            json.put("succ", true);
            json.put("stmt", "添加成功");
            toJson(json.toString());

    }
}
